package business.global.pk.nn;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

/**
 * 十三水，设置牌
 * 
 * @author Huaxing
 *
 */
public class NNSetCard {

	public ArrayList<Integer> leftCards = new ArrayList<>(); // 扑克牌编号
	private Random random;
	private int laizi;
	NNRoomSet set;

	public NNSetCard(NNRoomSet set) {
		random = new Random();
		this.set = set;
		this.randomCard();
	}

	/**
	 * 洗牌 zhaozw
	 */
	public void randomCard() {
		if (this.set.room.roomCfg.wanglaiwanfa == 0) {
			this.leftCards = BasePockerLogic.getRandomPockerList(1, 0, BasePocker.PockerListType.POCKERLISTTYPE_AFIRST);
		} else if (this.set.room.roomCfg.wanglaiwanfa == 1) {
			this.leftCards = BasePockerLogic.getRandomPockerList(1, 1, BasePocker.PockerListType.POCKERLISTTYPE_AFIRST);
		} else {
			this.leftCards = BasePockerLogic.getRandomPockerList(1, 1, BasePocker.PockerListType.POCKERLISTTYPE_AFIRST);
			// 随机癞子，不能是大小王

			laizi = this.leftCards.get(random.nextInt(this.leftCards.size()));
			while (BasePockerLogic.isTrump(laizi)) {
				laizi = this.leftCards.get(random.nextInt(this.leftCards.size()));
			}
		}
	}

	/*
	 * 洗牌
	 **/
	public void onXiPai() {
		Collections.shuffle(this.leftCards);
	}

	/**
	 * 发牌
	 * 
	 * @param cnt
	 * @return
	 */
	public ArrayList<Integer> popList(int cnt) {
		ArrayList<Integer> ret = new ArrayList<Integer>();
		// 检查剩余牌数是否足够
		if (this.leftCards.isEmpty() || this.leftCards.size() < cnt) {
			System.out.println("警告：剩余牌数不足！需要 " + cnt + " 张，实际剩余 " + this.leftCards.size() + " 张");
			return ret;
		}

		for (int i = 0; i < cnt; i++) {
			Integer Integer1 = this.leftCards.remove(random.nextInt(this.leftCards.size()));
			ret.add(Integer1);
		}
		return ret;
	}

	public ArrayList<Integer> getLeftCards() {
		return leftCards;
	}

	public int getLaizi() {
		return laizi;
	}

	/**
	 * 从牌堆中选出好牌
	 * 阳阳改
	 * 
	 * @param cnt 需要发的牌数量（通常是5）
	 * @return 返回选出的好牌列表
	 */
	public ArrayList<Integer> popSpecificCards(int cnt, int dianshu) {
		// ArrayList<Integer> ret = new ArrayList<Integer>();
		// ret.add(0x02);
		// ret.add(0x03);
		// ret.add(0x05);
		// ret.add(0x0D);
		// ret.add(0x1D);
		// return ret;

		// ArrayList<Integer> ret = new ArrayList<Integer>();
		// ArrayList<Integer> tempCards = new ArrayList<>(this.leftCards);
		// for (int i = 0; i < cnt; i++) {
		// Integer Integer1 =
		// this.leftCards.remove(random.nextInt(this.leftCards.size()));
		// ret.add(Integer1);
		// }

		// int cardType = NNGameLogic.GetCardType(ret, this.laizi);
		// ArrayList<Integer> logicValues = new ArrayList<>();
		// for (Integer card : ret) {
		// logicValues.add(NNGameLogic.GetCardLogicValue(card));
		// }
		// System.out.println("当前牌型: " + cardType + ", 牌: " + logicValues);
		// if (cardType < 10) {
		// Collections.shuffle(tempCards);
		// this.leftCards = tempCards;
		// popSpecificCards(cnt);
		// }

		// return ret;
		if (dianshu == 0) {
			System.out.println("没有设置点数，使用普通发牌");
			return popList(cnt);
		}

		ArrayList<Integer> ret = new ArrayList<Integer>();
		int maxTries = 50; // 最大尝试次数，防止死循环
		int currentTry = 0;

		while (currentTry < maxTries) {
			// 从牌堆中随机选择cnt张牌
			ret.clear();
			ArrayList<Integer> tempCards = new ArrayList<>(leftCards);
			// 打乱临时牌组顺序
			Collections.shuffle(tempCards);
			for (int i = 0; i < Math.min(cnt, tempCards.size()); i++) {
				Integer card = tempCards.remove(random.nextInt(tempCards.size()));
				ret.add(card);
			}
			// 创建临时数组存储转换后的牌值
			ArrayList<Integer> logicValues = new ArrayList<>();
			for (Integer card : ret) {
				logicValues.add(NNGameLogic.GetCardLogicValue(card));
			}
			// 获取这手牌的牛牛点数
			int cardType = NNGameLogic.GetCardType(ret, this.laizi);
			System.out.println("当前尝试的牌型: " + cardType + ", 牌: " + logicValues);
			// 如果点数大于等于8，接受这手牌
			if (cardType >= dianshu) { // 0表示牛牛
				// 从原始牌堆中移除这些牌
				for (Integer card : ret) {
					this.leftCards.remove(card);
				}
				return ret;
			}
			// 输出当前尝试的牌型到控制台

			currentTry++;
		}

		// 如果多次尝试都没找到好牌，就用普通的发牌方式
		return popList(cnt);
	}
}
